<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/21
 * Time: 上午11:44
 */

namespace App\Http\Services;


use App\Http\Model\Guard;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\Hint;

class RoomWaitbitRadioServices extends RoomWaitbitServices
{

    protected $modelRoomHostMicro;
    protected $modelGuard;

    public function __construct()
    {
        $this->modelRoomHostMicro = new RoomHostMicro();
        $this->modelGuard = new Guard();
        parent::__construct();
    }

    public function allowMicros($room_id, $guard_type = 0)
    {
        $micros = parent::allowMicros($room_id, $guard_type); // TODO: Change the autogenerated stub
        foreach ($micros as $k => $v) {
            if ($guard_type == 1 && $v['microphone_id'] != 4 && $v['microphone_id'] != 5 && $v['microphone_id'] != 6 && $v['microphone_id'] != 7) {
                unset($micros[$k]);
            }
            if ($guard_type == 2 && $v['microphone_id'] != 1 && $v['microphone_id'] != 2 && $v['microphone_id'] != 3) {
                unset($micros[$k]);
            }
            if ($guard_type == 3 && $v['microphone_id'] != 0) {
                unset($micros[$k]);
            }
        }
        return array_values($micros);
    }


    public function get_users($roomId)
    {
        $hostMicroUserId = $this->modelRoomHostMicro->get_user_id($roomId);
        if (empty($hostMicroUserId)) {
            return [];
        }
        $userArr = parent::get_users($roomId);
        $guardArr = $this->modelGuard->getGuardList([
            ['guard_user_id', '=', $hostMicroUserId],
            //['guard_type_id', '=', $guardTypeId],
            ['expire_time', '>', time()],
        ]);
        $guardMap = [];
        foreach ($guardArr as $guard) {
            if (!isset($guardMap[$guard['user_id']])) {
                $guardMap[$guard['user_id']] = [];
            }
            $userId = $guard['user_id'];

            unset($guard['guard_id']);
            unset($guard['user_id']);
            unset($guard['guard_user_id']);
            unset($guard['guard_type_id']);
            //unset($guard['type']);
            $guardMap[$userId][] = $guard;
        }
        $retData = [];
        foreach ($userArr as $user) {
            if (array_key_exists($user['user_id'], $guardMap)){
                $user['guard_attribute'] = $guardMap[$user['user_id']];
                $retData[] = $user;
            }
        }
        return $retData;
    }


    public function apply($room_id, $guardType, &$user_id)
    {
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        $hostMicroUserId = $this->model_room_hostmicro->get_user_id($room_id);
        if (empty($hostMicroUserId)){
            return Hint::ROOM_HOSTMICRO_IS_NULL; //主持麦无人，无法上麦
        }
        $hasGuard = $this->modelGuard->hasGuard($user_id, $hostMicroUserId, $guardType);
        if (!$hasGuard){
            return Hint::ROOM_MICRO_GUARD_MATCH; //没有麦守护类型
        }
        return parent::apply($room_id, $guardType, $user_id); // TODO: Change the autogenerated stub
    }
}